package com.cloudz.iptv.bing;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.cloudz.iptv.MainActivity;
import com.cloudz.iptv.R;
import com.cloudz.iptv.activity.BaseActivity;
import com.cloudz.iptv.api.ApiConstants;
import com.cloudz.iptv.api.v1.BingServiceApi;
import com.cloudz.iptv.utils.DisplayUtils;
import com.cloudz.iptv.utils.InPutJson;
import com.cloudz.iptv.utils.ResponseUtils;
import com.cloudz.iptv.utils.RetrofitUtils;
import com.cloudz.iptv.utils.SimplePreference;
import com.cloudz.qr.QRCodeUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @File Name: com.cloudz.iptv.bing
 * @Author: YJ //作者及
 * @Date: 2018/12/28 12:00 AM//完成日期
 * @Description: // 显示二维码
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class BingQrActivity extends BaseActivity
{

    @BindView(R.id.bing_image_qr)
    ImageView imageView;

    private String deviceId;

    BingServiceApi bingServiceApi;

    private final static String TOKEN = "token";

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_bing_qr;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.deviceId = SimplePreference.getDeviceId(this);


        this.bingServiceApi = RetrofitUtils.getInstance().createApi(SimplePreference.getServerUrl(this), BingServiceApi.class);

        Intent intent = getIntent();

        Bitmap mBitmap = QRCodeUtil.createQRCodeBitmap(intent.getStringExtra("qr"), DisplayUtils.dp2px(this, 100));

        imageView.setImageBitmap(mBitmap);

        timer.schedule(task, 1000, 1000); // 1s后执行task,经过1s再次执行
    }

    Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            if (msg.what == 1)
            {
                stopTimer();

                Bundle bundle = msg.getData();
                String token = bundle.getString(TOKEN);
                SimplePreference.putString(BingQrActivity.this, ApiConstants.BING_TOKEN, token);
                Intent intent = new Intent(BingQrActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            super.handleMessage(msg);
        }
    };

    Timer timer = new Timer();

    TimerTask task = new TimerTask()
    {

        @Override
        public void run()
        {

            // 验证绑定情况
            bingServiceApi.validateBind(deviceId).enqueue(new Callback<ResponseBody>()
            {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
                {

                    int code = response.code();
                    if (code == 200)
                    {
                        try
                        {
                            InPutJson inOutJson = ResponseUtils.getInOutJson(response);

                            if (inOutJson.result)
                            {
                                Message message = new Message();
                                message.what = 1;
                                Bundle bundle = new Bundle();
                                String token = inOutJson.data;
                                bundle.putString(TOKEN, token);
                                ApiConstants.IPTV_TOKEN = token;
                                message.setData(bundle);
                                handler.sendMessage(message);
                            }
                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }

                    } else
                    {
                        showToast("重试失败");
                    }

                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t)
                {
                    showToast("重试失败");
                }

            });
        }

    };

    private void stopTimer()
    {
        if (timer != null)
        {
            timer.cancel();
            timer = null;
        }
        if (task != null)
        {
            task.cancel();
            task = null;
        }
    }
}